<?php

/*
 * Object/PlanningTemplate.php
 */

namespace RHX\Model\Object;

use Zend\Db\Adapter\Adapter;

class PlanningTemplate {

    public $id;
    public $site_id;
    public $template;
    public $libelle;
    public $creation;
    public $edition;
    public $parent;
    public $actif;
    protected $adapter;

    public function __construct(Adapter $adapter, $data = array()) {
	$this->exchangeArray($data);
	$this->adapter = $adapter;
    }

    public function exchangeArray($data) {
	$this->id	 = isset($data['id']) ? $data['id'] : null;
	$this->site_id	 = isset($data['site_id']) ? $data['site_id'] : 0;
	$this->template	 = isset($data['template']) ? $data['template'] : 0;
	$this->libelle	 = isset($data['libelle']) ? $data['libelle'] : '';
	$this->creation	 = isset($data['creation']) ? $data['creation'] : '0000-00-00';
	$this->edition	 = isset($data['edition']) ? $data['edition'] : '0000-00-00';
	$this->parent = isset($data['parent']) ? (int)$data['parent'] : 0;
	$this->actif = isset($data['actif']) ? (int)$data['actif'] : 0;
    }

    public function getArrayCopy() {
	return get_object_vars($this);
    }

    /**
     *
     * @return Site
     */
    public function getSite() {
	// Table Site
	$siteTable = new \RHX\Model\Table\SiteTable($this->adapter);

	return $siteTable->get($this->site_id);
    }

    /**
     * Récupérer les templates correspondant au numéro de template
     *
     * @return \Zend\Db\ResultSet\ResultSet
     */
    public function getTemplates() {
	// table employe template
	$tableTemplate	 = new \RHX\Model\Table\EmployeTemplateTable($this->adapter);
	$templates	 = $tableTemplate->getTemplates($this->template);

	return $templates;
    }

    /**
     * Version du template
     *
     * @return int
     */
    public function getVersion() {
	/**
	 * @var EmployeTemplate Planning Type de l'employé
	 */
	$template	 = $this->getTemplates()
		->current();

	return $template->getVersion();
    }

    /**
     * Ajoute une version au planning type
     * 
     * @return int
     */
    public function addVersion() {
	$templates	 = $this->getTemplates();
	$version	 = 0;
	foreach ($templates as $tpl) {
	    $version = $tpl->addVersion();
	}

	return $version;
    }
    
    /**
     * Récupérer le Planning Template Parent
     * 
     * @return PlanningTemplate
     */
    public function getParent(){
	$table = new \RHX\Model\Table\PlanningTemplateTable($this->adapter);
	try{
	    return $table->get($this->parent);
	}catch(\Exception $ex){
	    return null;
	}
    }

}

?>
